package org.sy.lab.多线程基础._1_管理._2_状态切换;

import java.util.concurrent.TimeUnit;

/**
 * 实现Runnable来创建线程
 * 优点是可以继承其他的类，可以作为多个Thread的target实现资源共享
 */
public class MyThread implements Runnable {
	
	private Object lock;
	
	public MyThread(Object lock) {
		this.lock = lock;
	}
	
	@Override
	public void run() {
		try {
			TimeUnit.SECONDS.sleep(1);
			synchronized (lock) {
				System.out.println("线程运行：进入等待lock");
				lock.wait();
			}
			
			TimeUnit.SECONDS.sleep(1);
			System.out.println("线程运行：进入超时等待");
			
			TimeUnit.SECONDS.sleep(3);
			System.out.println("线程运行：进入阻塞lock");
			synchronized (lock) {
				TimeUnit.SECONDS.sleep(1);
				System.out.println("线程运行：获得锁");
			}
			TimeUnit.SECONDS.sleep(1);
			System.out.println("线程运行：无限运行");
			boolean flag = true;
			do {
				if(Thread.interrupted()) {
					flag = false;
					//System.out.println("线程运行：线程中断");
				}
			} while(flag);
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("线程运行：结束执行");
	}
}
